package com.yw.android.common.utils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * 类转换初始化
 */
public class TUtil {
    public static <T> T getT(Object o, int i) {
        try {
            Type mType = (o.getClass().getGenericSuperclass());
            if (!(mType instanceof ParameterizedType)) {
                return null;
            }
            Type[] types = ((ParameterizedType) mType).getActualTypeArguments();
            return ((Class<T>) types[i]).newInstance();
        } catch (InstantiationException e) {
            LOG.e("[Object : " + o + "][i : " + i + "] getT (InstantiationException)" + e);
        } catch (IllegalAccessException e) {
            LOG.e("[Object : " + o + "][i : " + i + "] getT (IllegalAccessException)" + e);
        } catch (ClassCastException e) {
            LOG.e("[Object : " + o + "][i : " + i + "] getT (ClassCastException)" + e);
        }
        return null;
    }

    public static Class<?> forName(String className) {
        try {
            return Class.forName(className);
        } catch (ClassNotFoundException e) {
            LOG.e("[className : " + className + "] forName (ClassNotFoundException)" + e);
        }
        return null;
    }
}
